package com.qijian.maindata.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qijian.common.utils.DateUtils;
import com.qijian.common.utils.SecurityUtils;
import com.qijian.maindata.domain.query.TeamQuery;
import com.qijian.maindata.domain.vo.TeamVo;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
    import java.util.ArrayList;

    import com.qijian.common.utils.StringUtils;
    import org.springframework.transaction.annotation.Transactional;
    import com.qijian.maindata.domain.TeamMember;
import com.qijian.maindata.mapper.TeamMapper;
import com.qijian.maindata.domain.Team;
import com.qijian.maindata.service.ITeamService;

/**
 * 生产班组Service业务层处理
 *
 * @author qijian
 * @date 2025-04-01
 */
@Service
@RequiredArgsConstructor
public class TeamServiceImpl extends ServiceImpl<TeamMapper, Team> implements ITeamService {

    private final TeamMapper teamMapper;


    @Override
    public Page<TeamVo> pageVo(Page<Team> page, Wrapper<TeamQuery> queryWrapper) {
        return teamMapper.selectPageVo(page, queryWrapper);
    }

    @Override
    public List<TeamVo> listVo(Wrapper<TeamQuery> queryWrapper) {
        return teamMapper.selectPageVo(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(Team team) {
        int result = teamMapper.insert(team);
        team.getTeamMemberList().forEach(member -> member.setTeamId(team.getTeamId()));
        teamMapper.saveDetail(team.getTeamMemberList());
        return result > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(Team team) {
        team.setUpdateBy(SecurityUtils.getNickName());
        int result = teamMapper.updateTeam(team);
        team.getTeamMemberList().forEach(member -> member.setTeamId(team.getTeamId()));
        teamMapper.delDetail(team.getTeamId());
        teamMapper.saveDetail(team.getTeamMemberList());
        return result > 0;
    }

    @Override
    public TeamVo getInfoById(Long teamId) {
        return teamMapper.getInfoById(teamId);
    }
}
